package com.zhuojy.equity.mall.admin.web.controller;

import com.zhuojy.equity.mall.admin.common.exception.ExceptionUtil;
import com.zhuojy.equity.mall.admin.common.support.Result;
import com.zhuojy.equity.mall.admin.common.utils.LogUtil;
import com.zhuojy.equity.mall.admin.service.ResourceService;
import com.zhuojy.equity.mall.admin.service.dto.req.CreateResourceReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.DeleteResourceByIdReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.ModifyResourceReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.QueryResourceByIdReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.CreateResourceResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.DeleteResourceByIdResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.ModifyResourceResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.QueryAllResourceResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.QueryResourceByIdResDTO;
import com.zhuojy.equity.mall.admin.web.security.component.DynamicSecurityMetadataSource;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台资源管理Controller
 */
@RestController
@RequestMapping("/resource")
@RequiredArgsConstructor
public class ResourceController {

	static Logger log = LoggerFactory.getLogger(ResourceController.class);

	private final ResourceService resourceService;

	private final DynamicSecurityMetadataSource dynamicSecurityMetadataSource;

	/**
	 * 添加后台资源
	 */
	@PostMapping(value = "/createResource")
	public Result<CreateResourceResDTO> createResource(@RequestBody CreateResourceReqDTO createResourceReqDTO) {
		String methodName = "添加后台资源";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, createResourceReqDTO);
		Result<CreateResourceResDTO> result;
		try {
			result = resourceService.createResource(createResourceReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 修改后台资源
	 */
	@PostMapping(value = "/modifyResource")
	public Result<ModifyResourceResDTO> modifyResource(@RequestBody ModifyResourceReqDTO modifyResourceReqDTO) {
		String methodName = "修改后台资源";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, modifyResourceReqDTO);
		Result<ModifyResourceResDTO> result;
		try {
			dynamicSecurityMetadataSource.clearDataSource();
			result = resourceService.modifyResource(modifyResourceReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 根据ID查询资源详情
	 */
	@PostMapping(value = "/queryResourceById")
	public Result<QueryResourceByIdResDTO> queryResourceById(@RequestBody QueryResourceByIdReqDTO queryResourceByIdReqDTO) {
		String methodName = "根据ID查询资源详情";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, queryResourceByIdReqDTO);
		Result<QueryResourceByIdResDTO> result;
		try {
			result = resourceService.queryResourceById(queryResourceByIdReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 根据ID删除后台资源
	 */
	@PostMapping(value = "/delete/{id}")
	public Result<DeleteResourceByIdResDTO> deleteResourceById(@RequestBody DeleteResourceByIdReqDTO deleteResourceByIdReqDTO) {
		String methodName = "根据ID查询资源详情";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, deleteResourceByIdReqDTO);
		Result<DeleteResourceByIdResDTO> result;
		try {
			dynamicSecurityMetadataSource.clearDataSource();
			result = resourceService.deleteResourceById(deleteResourceByIdReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 查询所有后台资源
	 */
	@PostMapping(value = "/queryAllResource")
	public Result<QueryAllResourceResDTO> queryAllResource() {
		String methodName = "查询所有后台资源";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, null);
		Result<QueryAllResourceResDTO> result;
		try {
			result = resourceService.queryAllResource();
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}
}
